<?php

  // Set the format for printing timestamps
  define("TS_FORMAT", "yyyy-mm-dd HH24:mi:sstzh");

  function get_recordset($connection, $sql) {
    //assume we always want ordered array (not associative)
    //note, this will return the whole recordset no matter how large

    //echo "\nEntered get_recordset...\n";
    $result = $connection->getAll($sql, array(), DB_FETCHMODE_ORDERED);
		//echo "{$sql}";
    if (DB::isError($result)) {
	//echo "{$result->getUserInfo()}";
	//echo "{$result->getDebugInfo()}";
      trigger_error($result->getUserInfo(), E_USER_ERROR);
    }
    //echo "\nafter getAll\n";
    //print_r($result);
    
    return $result;
  }

  function get_recordcount($connection, $sql) {
    //assume we always want ordered array (not associative)
    //note, this will return the whole recordset no matter how large

    $countQuery = "SELECT COUNT(*) AS total FROM ($sql) sub";
    //echo $countQuery;
    $rcount = get_scalar(get_recordset($connection, $countQuery));

    return $rcount;
  }

  function get_limitclause($pn, $rcount) {
  
        //first figure out how many pages we have
      $pageCount = (int)(ceil($rcount/DISPLAY_ROWS));
      //make sure the current page is valid...
      if ($pn > $pageCount) $pn = $pageCount;
      if ($pn < 1) $pn = 1;

	  $clause = " LIMIT ". (DISPLAY_ROWS) ." OFFSET ". ($pn - 1) * (DISPLAY_ROWS);
      return $clause;
  }

  function do_pagination_rewriting($pn, $rcount, $origSql) {
	
    $dbType = $GLOBALS['dsn']['phptype'];

    if ($dbType == "oci8") {
        //first figure out how many pages we have
      $pageCount = (int)(ceil($rcount/DISPLAY_ROWS));
      //make sure the current page is valid...
      if ($pn > $pageCount) $pn = $pageCount;
      if ($pn < 1) $pn = 1;
      $minRow = ($pn - 1) * (DISPLAY_ROWS);
      $maxRow = $minRow + DISPLAY_ROWS;
      	
      //in oracle we rewrite this to use subqueries and rownum and stuff

      return "SELECT * FROM (" .
                "SELECT a.*, ROWNUM rnum FROM " .
                 "(" . $origSql . ") a " . 
                "WHERE ROWNUM <= " . $maxRow . ") " .
             "WHERE rnum > " . $minRow; 

    }
    if ($dbType == "pgsql") {
	//in postgres just add in the limit clause
      return $origSql . get_limitclause($pn, $rcount);
    }  	
    trigger_error("Unsupported db type in utils.inc.php.get_interval!",
	E_USER_ERROR);

  }

  function get_scalar($result) {
    //echo "\nentered get_scalar...\n";
    if (count($result) == 1) {
      $answer = $result[0];
      $answer = $answer[0];
      //echo "\about to return $answer from get_scalar\n";
      return $answer;
    } else {
      //we should always have 1 row
      trigger_error("Did not return exactly 1 row in get_scalar", 
        E_USER_ERROR);

      return "N/A";
    }    

  }

  function get_interval($interval_scalar, $interval_unit) {

    $dbType = $GLOBALS['dsn']['phptype'];

    if ($dbType == "oci8") {
      //this is so disgusting.  we need to tell oracle the 'precision' on the
      //year units.  just always set it at 5 for now...
      if (strnatcasecmp($interval_unit, "year") == 0) {
	$interval_unit = "year(5)";
      }
      return "interval '{$interval_scalar}' {$interval_unit}";
    }
    if ($dbType == "pgsql") {
      return "interval '{$interval_scalar} {$interval_unit}'";
    }  	
    trigger_error("Unsupported db type in utils.inc.php.get_interval!",
	E_USER_ERROR);
  }

  function get_parsed_gjid_sql() {

    $dbType = $GLOBALS['dsn']['phptype'];

    if ($dbType == "oci8") {
       return
       "REGEXP_SUBSTR(mca.globaljobid,'[^#]+',1,1) as schedd, " .
       "REGEXP_SUBSTR(REGEXP_SUBSTR(mca.globaljobid,'[^#]+',1,3),'[^.]+',1,1) as cid, " .
       "REGEXP_SUBSTR(REGEXP_SUBSTR(mca.globaljobid,'[^#]+',1,3),'[^.]+',1,2) as pid ";
    }
    if ($dbType == "pgsql") {
       return
       "split_part(mca.globaljobid,'#',1) as schedd, " .
       "split_part(split_part(mca.globaljobid,'#',3),'.',1) as cid, " .
       "split_part(split_part(mca.globaljobid,'#',3),'.',2) as pid ";
      
    }  	
    trigger_error("Unsupported db type in utils.inc.php.get_interval!",
	E_USER_ERROR);
    
  }

?>
